package com.ptszyxx.popose.module.main.message.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.enums.MessageNoticeEnum;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.notice.NoticeEntity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

public class MessageNoticeVM extends BaseViewModel<CommonRepository> {
    public MessageNoticeEnum noticeEnum;

    public MessageNoticeVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCloseCommand = new BindingCommand(() ->
        YActivityUtil.getInstance().close(this)
    );
    //=========================================接口：start===========================================

    public void requestNotice(boolean refresh){
        if (refresh){
            page = 1;
        }else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));

        Observable observable = model.messageSystem(map);
        if (noticeEnum == MessageNoticeEnum.platform){
            observable = model.messagePlatform(map);
        }

        HttpUtils.getInstance()
                .list(observable, this,
                        new OnSuccessListResult<NoticeEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<NoticeEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 详情
     */
    public void jumpMessageNoticeDetail(NoticeEntity entity){
        YActivityUtil.getInstance().jumpMessageNoticeDetails(this, entity.getMessage(), noticeEnum);
    }

}
